#include "ProgressDialog.h"

/*********************| marcos |*********************/

#define TEXT_COLOR			ccc3(167, 198, 224)
#define PROGRESS_BG_SPACE		12

/*********************| static datas |*********************/

static ProgressDialog*			_defaulgDialog = NULL;
static int				_keyStatus = -1;

/*********************| funcs in ProgressDialog |*********************/

ProgressDialog*ProgressDialog::create(const char* str)
{
	ProgressDialog* ret = new ProgressDialog();
	if (ret && ret->init(str)) {
		ret->autorelease();
		return ret;
	}
	CC_SAFE_DELETE(ret);
	return NULL;
}

void ProgressDialog::hide()
{
	CCScene* runs = CCDirector::sharedDirector()->getRunningScene();
	if (runs) {
		CCLayer* player = (CCLayer*)runs->getChildren()->objectAtIndex(0);
		if (player && _keyStatus != -1) {
			player->setKeypadEnabled(1 == _keyStatus);
		}
	}
	this->removeFromParentAndCleanup(true);
}

size_t ProgressDialog::tipsLength() const
{
	return strlen(_tip->getString());
}

ProgressDialog*ProgressDialog::defaultDialog()
{
	if (!_defaulgDialog) {
		_defaulgDialog = ProgressDialog::create(NULL);
		_defaulgDialog->retain();
	}
	return _defaulgDialog;
}

ProgressDialog::ProgressDialog()
{ 
	_dialogBg = NULL;
	_progress = NULL;
	_tip = NULL;
}

bool ProgressDialog::init(const char* str)
{
	bool bRet = false;
	do {
		CC_BREAK_IF(!ModalLayer::init(0));
		_dialogBg = CCSprite::create("dialog/progress_bg.png");
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		_dialogBg->setPosition(ccp(winSize.width / 2, winSize.height * 3 / 5));
		CC_BREAK_IF(!_dialogBg);
		this->addChild(_dialogBg);

		_progress = CCSprite::create("dialog/progress.png");
		CC_BREAK_IF(!_progress);
		this->addChild(_progress);
		_progress->runAction(CCRepeatForever::create(CCRotateBy::create(0.7f, -360.0f)));
		_progress->setPosition(_dialogBg->getPosition());

		if (!str) {
			str = "";
		}
		_tip = CCLabelTTF::create(str, "DroidSansFallbackFull", 18, CCSizeMake(250, 108),
					  kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
		CC_BREAK_IF(!_tip);
		_tip->setColor(TEXT_COLOR);
		_tip->setAnchorPoint(ccp(0, 0.5f));
		_tip->setPosition(ccp(_dialogBg->boundingBox().getMinX() +
				   2 * PROGRESS_BG_SPACE +
				   _progress->getContentSize().width, _dialogBg->getPositionY()));
		this->addChild(_tip);
		setTips(str);

		bRet = true;
	}while(0);
	return bRet;
}

void ProgressDialog::setTips(const char* str)
{
	if (!str) {
		str = "";
	}
	if (!strlen(str)) {
		_progress->setPositionX(_dialogBg->getPositionX());
	} else {
		_progress->setPositionX(_dialogBg->boundingBox().getMinX() +
					_progress->getContentSize().width / 2 +
					PROGRESS_BG_SPACE);
	}
	if (strcmp(str, _tip->getString())) {
		_tip->setString(str);
	}
}

int ProgressDialog::show()
{
	int ret = ModalLayer::show();
	if (0 == ret) {
		CCScene* runs = CCDirector::sharedDirector()->getRunningScene();
		if (runs) {
			CCLayer* player = (CCLayer*)runs->getChildren()->objectAtIndex(0);
			if (player) {
				_keyStatus = player->isKeypadEnabled() ? 1 : 0;
				player->setKeypadEnabled(false);
			}
		} else {
			_keyStatus = -1;
		}
		_progress->runAction(CCRepeatForever::create(CCRotateBy::create(0.7f, -360.0f)));
	}
	return ret;
}
